package com.antdesignbackend.backend.controller;

import com.antdesignbackend.backend.aop.AuthToken;
import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.Settings;
import com.antdesignbackend.backend.entity.entityVo.SettingVo;
import com.antdesignbackend.backend.service.SettingService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

@Controller
@RequestMapping("/setting")
public class SettingController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    ReturnVO result = new ReturnVO();
    private Integer menuId;
    private Integer userId;
    private String roleID;
    @Autowired
    private SettingService settingService;
    /**
     * 通知间隔设置
     */
    @GetMapping("/notifyIndex")
    @AuthToken
    public String notifyIndex(String menuId,HttpServletRequest request) {
        this.menuId = Integer.parseInt(menuId);
        //获取cookies信息 roleID 1超级管理员，2管理员，3商户
        userId=(Integer) request.getSession().getAttribute("UserID");
        roleID=(String) request.getSession().getAttribute("RoleID");
        return "setting/notifyIndex";
    }
    /**
     * 断电时间设置
     */
    @GetMapping("/offtimeIndex")
    @AuthToken
    public String offtimeIndex(String menuId,HttpServletRequest request) {
        this.menuId = Integer.parseInt(menuId);
        //获取cookies信息 roleID 1超级管理员，2管理员，3商户
        userId=(Integer) request.getSession().getAttribute("UserID");
        roleID=(String) request.getSession().getAttribute("RoleID");
        return "setting/offtimeIndex";
    }
    /***
     * 根据id
     */
    @GetMapping("/getById/{id}")
    @ResponseBody
    public ReturnVO getById(@PathVariable Serializable id) {
        result = new ReturnVO();
        Settings data=settingService.getById(id);
        result.setData(data);
        if (data!=null) {
            result.setMessage("成功");
            result.setCode("200");
        } else {
            result.setMessage("失败");
            result.setCode("500");
        }
        return result;
    }
    /***
     * 操作相关，通过action 区分
     * @return
     */
    @PostMapping("/action")
    @ResponseBody
    @AuthToken
    public ReturnVO actions(@RequestBody SettingVo vo) throws Exception {
        result = new ReturnVO();
        boolean b = false;
        String msg=null;
        switch (vo.getAction())
        {
            case "notityday":
                //通知间隔设置
                b=settingService.update(new UpdateWrapper<Settings>().set("notityDay1", vo.getNotityDay1()).set("notityDay2", vo.getNotityDay2()).set("notityDay3", vo.getNotityDay3()).eq("id", 1));

                break;
            case "offtime":
                //断电时间设置
                b=settingService.update(new UpdateWrapper<Settings>().set("offTime", vo.getOffTime()).eq("id", 1));

                break;
            default:
                break;
        }

        if(b==true){
            result.setCode("200");
            if(msg==null)
            {
                result.setMessage("保存成功！");
            }
            else
            {
                result.setMessage(msg);
            }

        }
        else
        {
            result.setCode("500");
            if(msg==null)
            {
                result.setMessage("保存失败！");
            }
            else
            {
                result.setMessage(msg);
            }

        }
        return result;
    }
}
